
import ControlScript from "../PublicScript/ControlScript";
import CoinAnim from "./CoinAnim";
import Music, {musicName} from "../PublicScript/Music";
import GameData_red_blue_man from "./GameData_red_blue_man";
import GetSkin from "./GetSkin";
import PowerControl from "../PublicScript/PowerControl";
import FlyPower from "./FlyPower";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Turntable extends cc.Component {

    @property(cc.Node)
    wheel: cc.Node = null;
    @property(cc.Node)
    point: cc.Node = null;
    @property(cc.Button)
    returnBtn: cc.Button = null;
    @property(cc.Node)
    skinNode: cc.Node = null;
    @property(cc.Node)
    goldNode: cc.Node = null;
    @property(cc.Node)
    probability_page: cc.Node = null;

    onLoad () {

    }

    start () {
        this.skinState();
    }

    //正在播放动画
    isAnim = false;

    click(){
        let self = this;
        Music.GamePlayEffect(musicName.click);
        if(this.isAnim){
            return;
        }
        this.isAnim = true;
        GameData_red_blue_man.showVideo({callback:()=>{
                self.rotateAnim()
            }, name: "转盘抽奖", callback1:()=>{
                this.isAnim = false;
            }});
    }

    showPro(e, d){
        this.probability_page.active = d == 1 ? true : false;
    }

    skinState(){
        this.skinNode.active = !(GameData_red_blue_man.skinData[6].unLock == 1);
        this.goldNode.active = !this.skinNode.active;
    }

    //旋转动画
    rotateAnim(){
        let randNUm = Math.round(Math.random() * 1000);
        let index = 0;
        let moneyNum = 0;
        let powerNum = 0;

        if(randNUm >= 0 && randNUm <= 309){             //30.9%
            index = 1;
            moneyNum = 200;
        }else if(randNUm >= 310  && randNUm <= 509){    //20%  309+200 = 509
            index = 2;
            // moneyNum = 500;
            powerNum = 2;
        }else if(randNUm >= 510  && randNUm <= 689){    //18%   509+180 = 689
            index = 3;
            moneyNum = 1000;
        }else if(randNUm >= 690  && randNUm <= 839){    //15%   689 + 150 = 839
            index = 4;
            // moneyNum = 2000;
            powerNum = 5;
        }else if(randNUm >= 840  && randNUm <= 939){    //10%   839 + 100 = 939
            index = 5;
            moneyNum = 3000;
        }else if(randNUm >= 940  && randNUm <= 989){    //5%    939 + 50 = 989
            index = 6;
            // moneyNum = 5000;
            powerNum = 8;
        }else if(randNUm >= 990  && randNUm <= 999){    //1%    989
            index = 7;
            moneyNum = 5000;
        }else if(randNUm == 1000){    //0.1%
            index = 8;
            moneyNum = 20000;
        }
        // index = 8;
        // moneyNum = 20000;

        let rotateAverage = 360/8;
        Music.GamePlayEffect(musicName.zhuanpan);
        let randRotate = (Math.random() *  (rotateAverage - 4) + 2) + (rotateAverage * (index -1));
        this.returnBtn.interactable = false;
        this.wheel.runAction(
            cc.sequence(
                cc.rotateTo(2, -(360*5 + randRotate)).easing(cc.easeInOut(2)),
                cc.callFunc(()=>{
                    let self = this;
                    if(index == 8 && GameData_red_blue_man.skinData[6].unLock == 0){
                        ControlScript.instantiate("getSkin",(res)=>{
                            res.getComponent(GetSkin).setInitData(7, 0);
                            GameData_red_blue_man.skinData[6].unLock = 1;
                            GameData_red_blue_man.saveSkinData();
                            this.skinState();
                            self.returnBtn.interactable = true;
                            this.isAnim = false;
                        });
                    }else{
                        if(powerNum == 0){
                            ControlScript.instantiate("coinAnim", (res)=>{
                                res.x = 250;
                                res.y = 100;
                                res.getComponent(CoinAnim).createCoin();
                                res.getComponent(CoinAnim).setPoint(this.point, moneyNum, true, ()=>{
                                    self.returnBtn.interactable = true;
                                    this.isAnim = false;
                                });
                            });
                        }else{
                            PowerControl.addPower(powerNum);
                            self.returnBtn.interactable = true;
                            this.isAnim = false;
                            let pos2 =cc.find("Canvas/power").children[0].children[0].convertToWorldSpaceAR(cc.v2(0,0));
                            let pos1 = this.wheel.children[index - 1].convertToWorldSpaceAR(cc.v2(0,0));
                            ControlScript.instantiate("flyPower",(res)=>{
                                res.getComponent(FlyPower).init(pos1, pos2, ()=>{

                                }, true)
                            })
                        }
                    }
                })
            )
        )
    }

    callback = null;
    setCallback(call){
        this.callback = call;
        console.log(this.callback);
    }

    onClick(){
        if(this.callback){
            this.callback();
        }else{
            this.node.destroy();
        }
    }

    // update (dt) {}
}
